package com.sigmazero13.MKDummy.activites;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

import com.sigmazero13.MKDummy.R;
import com.sigmazero13.MKDummy.components.ExpandableHeightGridView;
import com.sigmazero13.MKDummy.models.DummyPlayer;
import com.sigmazero13.MKDummy.models.MKCharacter;
import com.sigmazero13.MKDummy.utils.CharacterAdapter;

public class MKDummyMain extends Activity implements OnClickListener, OnDismissListener {
	
	public static int VOLKARE_SETUP_DIALOG;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        LinearLayout specialButtonView = (LinearLayout)findViewById(R.id.special_button_layout);
        
        ExpandableHeightGridView buttonView = (ExpandableHeightGridView)findViewById(R.id.player_choice_buttons);
        buttonView.setAdapter(new CharacterAdapter(this, MKCharacter.values(), this));
        buttonView.setExpanded(true);
        
        LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        
        Button randomButton = new Button(this);
        randomButton.setText(R.string.random_button);
        randomButton.setTextSize(getResources().getDimension(R.dimen.button_text_size));
        randomButton.setOnClickListener(this);
        randomButton.setLayoutParams(layoutParams);
        randomButton.setMinimumWidth(300);
        randomButton.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
        specialButtonView.addView(randomButton);
        
        Button volkareButton = new Button(this);
        MKCharacter volkare = MKCharacter.VOLKARE;
        volkareButton.setText(volkare.getName());
    	volkareButton.setTextSize(getResources().getDimension(R.dimen.button_text_size));
    	volkareButton.setOnClickListener(this);
    	volkareButton.setLayoutParams(layoutParams);
    	volkareButton.setMinimumWidth(300);
    	volkareButton.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
    	Drawable image = getResources().getDrawable(volkare.getFactionImageResourceId());
    	image.setAlpha(0xff);
    	CharacterAdapter.scaleImage(image, 50);
    	volkareButton.setCompoundDrawablePadding(25);
    	volkareButton.setCompoundDrawables(image, null, null, null);
    	specialButtonView.addView(volkareButton);
    	
    	Button restoreButton = (Button)findViewById(R.id.game_restore_button);
    	restoreButton.setOnClickListener(this);
    }
    
    @Override
    public void onResume() {
    	super.onResume();
    	updateSaveStatus();
    	refreshImageAlphas();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menu, menu);
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	Intent i;
    	switch(item.getItemId()) {
    		case R.id.menu_help_option:
    			i = new Intent(this, HelpActivity.class);
    			startActivity(i);
    			break;
    		case R.id.menu_about_option:
    			AlertDialog about = new AlertDialog.Builder(this)
    				.setTitle(R.string.about_title)
    				.setMessage(Html.fromHtml(getString(R.string.about_content)))
    				.create();
    			
    			about.show();
    			break;
    		case R.id.menu_settings_option:
    			i = new Intent(this, PlayerPreferences.class);
    			startActivity(i);
    			break;
    		default:
    			break;
    	}
    	
    	return true;
    }
       
    @Override
	public void onClick(View v) {
    	if (!(v instanceof Button)) return;
    	if (getString(R.string.restore_button).equals(((Button)v).getText())) {
    		Intent i = new Intent(this, MKDummyPlayerActivity.class);
    		i.putExtra(MKDummyPlayer.RESTORE_GAME, true);
    		startActivity(i);
    	} else {
    		String chosenCharacter = ((Button)v).getText().toString();
    		
    		if (chosenCharacter.equals(getResources().getString(R.string.random_button))) {
    			chosenCharacter = MKCharacter.getRandomCharacter(false).getName();
    		}
    		
    		if (chosenCharacter.equals(MKCharacter.VOLKARE.getName())) {
    			showDialog(VOLKARE_SETUP_DIALOG);
    			return;
    		}
    		Intent i = new Intent(this, MKDummyPlayerActivity.class);
    		i.putExtra(MKDummyPlayer.CHARACTER_NAME, chosenCharacter);
    		startActivity(i);
    	}
	}
    
    
    @Override
    public Dialog onCreateDialog(int id, Bundle args) {
    	if (id != VOLKARE_SETUP_DIALOG) return null;
    	
    	Dialog d = new VolkareSetupDialog(this, this);
    	return d;
    }
    
    
    @Override
    public void onDismiss(DialogInterface dialog) {
    	if (dialog instanceof VolkareSetupDialog) {
    		Intent i = new Intent(this, MKDummyPlayerActivity.class);
    		i.putExtra(MKDummyPlayer.CHARACTER_NAME, MKCharacter.VOLKARE.getName());
    		i.putExtra(MKDummyPlayer.NUM_WOUNDS, ((VolkareSetupDialog)dialog).getNumWounds());
    		startActivity(i);
    	}
    }
    
    
    private void updateSaveStatus() {
    	Button restoreButton = (Button)this.findViewById(R.id.game_restore_button);
    	TextView statusText = (TextView)findViewById(R.id.save_game_state);
    	
    	for (String file : this.fileList()) {
        	if (getString(R.string.save_file).equals(file)) {
        		
        		try {
	        		FileInputStream fis = this.openFileInput(getString(R.string.save_file));
	    			ObjectInputStream is = new ObjectInputStream(fis);
	    			DummyPlayer savedPlayer = (DummyPlayer)is.readObject();
	    			
	    			String status = savedPlayer.getName() + ": Round " + savedPlayer.getRound() + 
	    				", Turn " + savedPlayer.getTurnInRound();
	    			statusText.setVisibility(View.VISIBLE);
	    			statusText.setText(status);
	    			
	    			restoreButton.setVisibility(View.VISIBLE);
        		} catch (IOException ioe) {
        			Toast.makeText(this, R.string.restore_error, Toast.LENGTH_SHORT).show();
        			this.deleteFile(getString(R.string.save_file));
        		} catch (ClassNotFoundException cnfe) {
        			throw new RuntimeException(cnfe);
        		}
	    		
        		return;
        	}
        } 
    	
    	restoreButton.setVisibility(View.GONE);
    	statusText.setVisibility(View.GONE);
    }
    
    public void refreshImageAlphas() {
    	for (MKCharacter character : MKCharacter.values()) {
    		Drawable image = this.getResources().getDrawable(character.getFactionImageResourceId());
    		image.setAlpha(0xff);
    	}
    }
 
}